package cn.edu.ctbu.scoremg.service;

import cn.edu.ctbu.scoremg.dao.CourseRepository;
import cn.edu.ctbu.scoremg.entity.Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CourseService {
    @Autowired
    public CourseRepository courseRepository;
    public List<Course> getAll(){
        return courseRepository.findAll();
    }

    /**
     * 增
     * @param course
     * @return
     */
    public Course add(Course course){
        return courseRepository.save(course);
    }

    /**
     * 查，id
     * @param id
     * @return
     */
    public Course findById(Integer id){
        return courseRepository.findById(id).orElse(null);
    }

    /**
     * 查，课程名
     * @param cname
     * @return
     */
    public List<Course> findByCname(String cname){
        return courseRepository.findByCname(cname);
    }

    /**
     * 修改
     * @param course
     * @return
     */
    public Course update(Course course){
        return courseRepository.save(course);
    }

    /**
     * 删除
     * @param id
     */
    public void delete(Integer id){
        courseRepository.deleteById(id);
    }

    /**
     * 删多个
     * @param ids
     */
    @Transactional
    public void deleteByIds(List<Integer> ids){
        courseRepository.deleteByIds(ids);

    }

    /**
     * 分页
     * @param pageable
     * @return
     */
    public Page<Course> getAll(Pageable pageable){
        return courseRepository.findAll(pageable);
    }
    public Page<Course> getAll(Example<Course> example, Pageable pageable){
        return courseRepository.findAll(example,pageable);
    }
}
